import {createRouter, createWebHashHistory, type RouteRecordRaw, type NavigationGuardNext, type RouteLocationNormalized} from 'vue-router';
import modules from './modules';
import {isUserLoggedIn, hasUserPermission} from '@/plugin/auth'; // 假设这些函数在auth模块中实现

const routes: RouteRecordRaw[] = [
    {
        path: "/",
        name: 'App',
        component: () => import('@/layouts/default.vue'),
        meta: {title: '管理控制台'},
        children: modules
    },
    {
        path: '/:pathMatch(.*)*',
        name: '404',
        component: () => import('@/views/error/404.vue'),
        meta: {title: '404'}
    },
    {
        path: '/login',
        name: 'LoginUser',
        component: () => import('@/views/account/login.vue'),
        meta: {title: '登录'}
    },
    {
        path:'/enterprise',
        name:'Enterprise',
        component:()=>import('@/views/enterprise/index.vue'),
        meta:{
            title:'我的企业'
        }
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
});
// 添加导航守卫
router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
    if (to.path !== '/login') {
        if (!isUserLoggedIn()) {
            next('/login');
        } else if (!hasUserPermission()) {
            next('/'); // 或者其他无权限页面
        } else {
            next();
        }
    } else {
        next();
    }
});

export default router;